import node_helpers


MAX_RESOLUTION = 16384


class AreaList:

    @classmethod
    def INPUT_TYPES(s):
        return {
            "required": {"area_list": ("ALIST",)},
            "optional": {},
        }

    RETURN_TYPES = (
        "AREA",
        "AREA",
        "AREA",
        "AREA",
        "AREA",
    )
    RETURN_NAMES = (
        "a1",
        "a2",
        "a3",
        "a4",
        "a5",
    )

    FUNCTION = "handle"

    OUTPUT_NODE = True

    CATEGORY = "fcl/area_list"

    history_dict = {}

    def handle(self, area_list):
        print("area_list", area_list)
        a1 = a2 = a3 = a4 = a5 = {}
        if len(area_list) > 0:
            a1 = area_list[0]
        if len(area_list) > 1:
            a2 = area_list[1]
        if len(area_list) > 2:
            a3 = area_list[2]
        if len(area_list) > 3:
            a4 = area_list[3]
        if len(area_list) > 4:
            a5 = area_list[4]
        print(
            "a1~5",
            (
                a1,
                a2,
                a3,
                a4,
                a5,
            ),
        )
        return (
            a1,
            a2,
            a3,
            a4,
            a5,
        )


class FclArea:
    @classmethod
    def INPUT_TYPES(s):
        return {
            "required": {"area": ("AREA",)},
            "optional": {},
        }

    RETURN_TYPES = (
        "INT",
        "INT",
        "INT",
        "INT",
    )
    RETURN_NAMES = (
        "x",
        "y",
        "width",
        "height",
    )

    FUNCTION = "handle"

    OUTPUT_NODE = True

    CATEGORY = "fcl/area"

    history_dict = {}

    def handle(self, area):
        print("fcl area", area)
        width = int(area.get("width", 64))
        height = int(area.get("width", 64))
        x = int(area.get("x", 0))
        y = int(area.get("y", 0))
        return (
            x,
            y,
            width,
            height,
        )


class FclConditioningSetArea:
    @classmethod
    def INPUT_TYPES(s):
        return {
            "required": {
                "conditioning": ("CONDITIONING",),
                "area": ("AREA",),
                #   "width": ("INT", {"default": 64, "min": 64, "max": MAX_RESOLUTION, "step": 8}),
                #   "height": ("INT", {"default": 64, "min": 64, "max": MAX_RESOLUTION, "step": 8}),
                #   "x": ("INT", {"default": 0, "min": 0, "max": MAX_RESOLUTION, "step": 8}),
                #   "y": ("INT", {"default": 0, "min": 0, "max": MAX_RESOLUTION, "step": 8}),
                "strength": (
                    "FLOAT",
                    {"default": 1.0, "min": 0.0, "max": 10.0, "step": 0.01},
                ),
            }
        }

    RETURN_TYPES = ("CONDITIONING",)
    FUNCTION = "append"

    CATEGORY = "conditioning"

    def append(self, conditioning, area, strength):
        width = area.get("width", 64)
        height = area.get("height", 64)
        x = area.get("x", 64)
        y = area.get("y", 64)
        c = node_helpers.conditioning_set_values(
            conditioning,
            {
                "area": (height // 8, width // 8, y // 8, x // 8),
                "strength": strength,
                "set_area_to_bounds": False,
            },
        )
        return (c,)
